package com.cardone.platform.common.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.context.Contexts;
import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dto.EntityCodeDto;
import com.cardone.platform.common.service.EntityCodeService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 实体编号
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/common/entityCode/")
@Controller
public class EntityCodeController {

	/**
	 * 实体编号：删除
	 * 
	 * @param deleteEntityCode
	 *            实体编号对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
  public Callable<ReturnDataDto<String>> deleteJson(final EntityCodeDto deleteEntityCode) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityCodeService.class).deleteByIds(deleteEntityCode);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 实体编号：编辑
	 * 
	 * @param findEntityCode
	 *            实体编号对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<EntityCodeDto>> findByIdJson(final EntityCodeDto findEntityCode, final Model model) {
		return new Callable<ReturnDataDto<EntityCodeDto>>() {
			@Override
			public ReturnDataDto<EntityCodeDto> call() throws Exception {
				try {
					final EntityCodeDto entityCode = ContextHolder.getBean(EntityCodeService.class).findById(EntityCodeDto.class, findEntityCode);

					return ReturnDataUtils.newReturnData(entityCode);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, EntityCodeController.log);
				}
			}
		};
	}

	/**
	 * 实体编号：分页查询
	 * 
	 * @param paginationEntityCode
	 *            实体编号对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
  public Callable<ReturnDataDto<PaginationDto<EntityCodeDto>>> paginationByLikeCodeJson(final EntityCodeDto paginationEntityCode) {
		return new Callable<ReturnDataDto<PaginationDto<EntityCodeDto>>>() {
			public ReturnDataDto<PaginationDto<EntityCodeDto>> call() throws Exception {
				try {
					final PaginationDto<EntityCodeDto> entityCodePagination = ContextHolder.getBean(EntityCodeService.class).paginationByLikeCode(EntityCodeDto.class, paginationEntityCode);
				
					return ReturnDataUtils.newReturnData(entityCodePagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 实体编号：分页查询
	 * 
	 * @param paginationEntityCode
	 *            实体编号对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
  public void paginationHtml(final EntityCodeDto paginationEntityCode, final Model model) {
		final PaginationDto<EntityCodeDto> entityCodePagination = ContextHolder.getBean(EntityCodeService.class).paginationByLikeCode(EntityCodeDto.class, paginationEntityCode);
		
		model.addAttribute("entityCodePagination", entityCodePagination);
	}
	
	/**
	 * 实体编号：分页查询
	 * 
	 * @param findEntityCode
	 *            实体编号对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
  public void editHtml(final EntityCodeDto findEntityCode, final Model model) {
		final EntityCodeDto entityCode = ContextHolder.getBean(EntityCodeService.class).findById(EntityCodeDto.class, findEntityCode);
		
		model.addAttribute("entityCode", entityCode);
	}

	/**
	 * 实体编号：查询
	 * 
	 * @param findListEntityCode
	 *            实体编号对象
	 * @return 实体编号对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
  	public Callable<ReturnDataDto<List<EntityCodeDto>>> findListByLikeCodeJson(final EntityCodeDto findListEntityCode) {
		return new Callable<ReturnDataDto<List<EntityCodeDto>>>() {
			public ReturnDataDto<List<EntityCodeDto>> call() throws Exception {
				try {
					final List<EntityCodeDto> entityCodeList = ContextHolder.getBean(EntityCodeService.class).findListByLikeCode(EntityCodeDto.class, findListEntityCode);
				
					return ReturnDataUtils.newReturnData(entityCodeList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 实体编号：保存
	 * 
	 * @param saveEntityCode
	 *            实体编号对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
  public Callable<ReturnDataDto<String>> saveJson(final EntityCodeDto saveEntityCode) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(EntityCodeService.class).saveByIdOrCode(EntityCodeDto.class, saveEntityCode);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}
}
